package TreeHole.patches;

import TreeHole.mod.TreeHoleHelper;
import TreeHole.mod.TreeHoleMod;
import basemod.BaseMod;
import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.BlightHelper;
import com.megacrit.cardcrawl.helpers.EventHelper;
import com.megacrit.cardcrawl.random.Random;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import spireMapOverhaul.BetterMapGenerator;
import spireMapOverhaul.SpireAnniversary6Mod;
import spireMapOverhaul.abstracts.AbstractZone;
import spireMapOverhaul.zones.beastslair.BeastsLairZone;
import spireMapOverhaul.zones.keymaster.KeymasterZone;
import spireTogether.other.RoomDataManager;
import spireTogether.patches.network.RoomEntryPatch;
import spireTogether.util.SpireHelp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

public class JRPatch {
    //Act like it 兼容修复
    @SpirePatch(clz = AbstractDungeon.class,method = SpirePatch.CONSTRUCTOR, paramtypez = {String.class, String.class, AbstractPlayer.class, ArrayList.class})
    public static class ActLikeItNoCountPatch{
        @SpireInsertPatch(rloc = 22)
        public static SpireReturn<Void> Insert(AbstractDungeon _inst, String name, String levelId, AbstractPlayer p, ArrayList<String> newSpecialOneTimeEventList){
            if(BaseMod.hasModID("actlikeit:") && (TreeHoleHelper.contains(AbstractDungeon.id) ||TreeHolePatch.exitTreeHole)){
                treeholeDungeonTransitionSetup();
                protectedCallAbstractDungeon(_inst);
                _inst.initializeCardPools();
                if (AbstractDungeon.floorNum == 0) {
                    p.initializeStarterDeck();
                }

                _inst.initializePotions();
                BlightHelper.initialize();
                AbstractDungeon.screen = AbstractDungeon.CurrentScreen.MAP;
                AbstractDungeon.isScreenUp = true;

                return SpireReturn.Return();
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(clz = AbstractDungeon.class,method = "dungeonTransitionSetup")
    public static class TransitionNoHealPatch{
        @SpirePrefixPatch
        public static SpireReturn<Void> Prefix(){
            if(!BaseMod.hasModID("actlikeit:") && TreeHoleHelper.contains(AbstractDungeon.id)){
                treeholeDungeonTransitionSetup();

                return SpireReturn.Return();
            }
            return SpireReturn.Continue();
        }
    }

    //TogetherSpire 兼容修复
    @SpirePatch(
            clz = TreeHolePatch.class,
            method = "outerTreeHole",
            requiredModId = "spireTogether",
            optional = true)
    public static class TogetherSpirePatch{
        @SpirePostfixPatch
        public static void Postfix(){
            RoomDataManager.StorageFloor treeHoleFloor = RoomDataManager.getFloor();
            if(treeHoleFloor!=null){
                String actID = SpireHelp.Gameplay.GetCurrActName();
                HashMap<String, RoomDataManager.StorageFloor> floorMap = RoomDataManager.dungeon.get(RoomEntryPatch.infiniteCounter);
                if(floorMap!=null){
                    floorMap.remove(actID);
                }
            }
        }
    }

    private static void protectedCallAbstractDungeon(AbstractDungeon _inst){
        //generateMonsters();
        ReflectionHacks.privateMethod(AbstractDungeon.class, "generateMonsters").invoke(_inst);
        //initializeBoss();
        ReflectionHacks.privateMethod(AbstractDungeon.class, "initializeBoss").invoke(_inst);
        //setBoss(AbstractDungeon.bossList.get(0));
        ReflectionHacks.privateMethod(AbstractDungeon.class, "setBoss",String.class).invoke(_inst,AbstractDungeon.bossList.get(0));
        //initializeEventList();
        ReflectionHacks.privateMethod(AbstractDungeon.class, "initializeEventList").invoke(_inst);
        //initializeEventImg();
        ReflectionHacks.privateMethod(AbstractDungeon.class, "initializeEventImg").invoke(_inst);
        //initializeShrineList();
        ReflectionHacks.privateMethod(AbstractDungeon.class, "initializeShrineList").invoke(_inst);
    }

   private static void treeholeDungeonTransitionSetup(){
       AbstractDungeon.topPanel.unhoverHitboxes();
       AbstractDungeon.pathX.clear();
       AbstractDungeon.pathY.clear();
       EventHelper.resetProbabilities();
       AbstractDungeon.eventList.clear();
       AbstractDungeon.shrineList.clear();
       AbstractDungeon.monsterList.clear();
       AbstractDungeon.eliteMonsterList.clear();
       AbstractDungeon.bossList.clear();
       AbstractRoom.blizzardPotionMod = 0;
       if(!TreeHolePatch.exitTreeHole)
        TreeHoleHelper.triggerWhenEnterTreeHole(AbstractDungeon.id,TreeHoleHelper.getCurrentType());
       AbstractDungeon.topPanel.panelHealEffect();
       AbstractDungeon.dungeonMapScreen.map.atBoss = false;
   }

   //Spire Biomes兼容修复
    @SpirePatch(
            clz = TreeHolePatch.class,
            method = "enterTreeHole",
            requiredModId = "anniv6",
            optional = true)
    public static class Anniv6Patch{
        @SpirePostfixPatch
        public static void Postfix(String ID ,int holeType){
            //Logger.getGlobal().info("=== TreeHoleMod: ");
            if(SpireAnniversary6Mod.currentRunSeenZones != null){
                for(AbstractZone z : BetterMapGenerator.getActiveZones(AbstractDungeon.map)){
                    SpireAnniversary6Mod.currentRunSeenZones.remove(z.id);
                }
            }
            BetterMapGenerator.clearActiveZones();
        }
   }

   @SpirePatch(
           clz = BeastsLairZone.class,
           method = "canSpawn",
           requiredModId = "anniv6",
           optional = true
   )
    public static class CanSpawnPatch{
        @SpirePostfixPatch
       public static boolean Postfix(boolean _ret, BeastsLairZone _inst){
            return false;
        }
   }

    @SpirePatch(
            clz = KeymasterZone.class,
            method = "canSpawn",
            requiredModId = "anniv6",
            optional = true
    )
    public static class CanSpawnPatch2{
        @SpirePostfixPatch
        public static boolean Postfix(boolean _ret, BeastsLairZone _inst){
            return false;
        }
    }


//
//   @SpirePatch(
//           clz = BetterMapGenerator.class,
//           method = "generate",
//           requiredModId = "anniv6",
//           optional = true)
//   public static class PossibilityPatch{
//        @SpireInsertPatch(rloc = 69-52,localvars = {"possibleZones"})
//        public static void Insert(BetterMapGenerator _inst, Random rng, int width, int height, int pathDensity, List<AbstractZone> possibleZones){
//            String des = "";
//            for(AbstractZone z : possibleZones){
//                des += z.id + ", ";
//            }
//            Logger.getGlobal().info("========= Possibilities: " + des);
//        }
//   }
}
